package com.fw.vote.controller;

import com.fw.vote.model.Match;
import com.fw.vote.model.MatchPkInfo;
import com.fw.vote.model.Player;
import com.fw.vote.service.MatchPkInfoService;
import com.fw.vote.service.MatchService;
import com.fw.vote.service.PlayerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.*;

import static sun.audio.AudioPlayer.player;

@Controller
@RequestMapping("pk")
public class MatchPkInfoController {
    @Autowired
    MatchPkInfoService matchPkInfoService;

    @Autowired
    PlayerService playerService;

    @Autowired
    MatchService matchService;


//    @RequestMapping("topkinfo")
//    public ModelAndView topkinfo(MatchPkInfo matchPkInfo,ModelAndView mv){
//        List<MatchPkInfo> getList = matchPkInfoService.select(matchPkInfo);
//        mv.setViewName("matchpkinfo");
//        mv.addObject("getList",getList);
//        return mv;
//    }

    @RequestMapping("listpk")
    public ModelAndView select(MatchPkInfo matchPkInfo,ModelAndView mv){
//查询一条数据不用遍历出来matchPkInfo就是返回的数据
         List<MatchPkInfo> matchPkInfo3 = matchPkInfoService.select(matchPkInfo);
        MatchPkInfo matchPkInfo1 = matchPkInfoService.get(matchPkInfo);
//      把matchPkInfo2集合的数据找到
     List<MatchPkInfo> matchPkInfo2=matchPkInfoService.playerList(matchPkInfo);
//     ？？？数组遍历
     Set<Player> set=new HashSet<>();
//set歌手的信息
     for(MatchPkInfo m:matchPkInfo2){
        Player player=new Player();
         player.setPlayerId(m.getFirstPlayerId());
         player.setPlayerName(m.getFirstPlayer().getPlayerName());
         player.setPlayerImage(m.getFirstPlayer().getPlayerImage());
         player.setPlayerInfo(m.getFirstPlayer().getPlayerInfo());
         player.setVoteNumber(m.getFirstTicketCount());

         Player player2=new Player();
         player2.setPlayerId(m.getSecondPlayerId());
         player2.setPlayerName(m.getSecondPlayer().getPlayerName());
         player2.setPlayerImage(m.getSecondPlayer().getPlayerImage());
         player2.setPlayerInfo(m.getSecondPlayer().getPlayerInfo());
         player2.setVoteNumber(m.getSecondTicketCount());
         set.add(player);
         set.add(player2);
     }
        mv.setViewName("matchpkinfo");
        mv.addObject("matchPkInfo",matchPkInfo1);
        mv.addObject("matchPkInfo2",matchPkInfo3.get(0));
        /*list.sort(new Comparator<Player>() {
                      @Override
                      public int compare(Player o1, Player o2) {
                          if(o1.getVoteNumber()>o2.getVoteNumber()){
                            return -1;
                          }else if(o1.getVoteNumber()<o2.getVoteNumber()){
                              return 1;
                          }
                          return 0;
                      }
                  }
        );*/
//        https://www.cnblogs.com/charlypage/p/8975422.html   lambda表达式排序，根据o2.getVoteNumber()-o1.getVoteNumber()的返回值再用sort这个方法来排序

        List<Player> list=new ArrayList<>(set);
        list.sort(((o1, o2) -> o2.getVoteNumber()-o1.getVoteNumber()));
        mv.addObject("playerList",list);
        return mv;
    }

    @RequestMapping("/selectMatchPk")
    public ModelAndView selectMatchPk(MatchPkInfo matchPkInfo, ModelAndView mv, Player player, Match match){
        //List<MatchPk> matchPkList=matchPkService.selectMatchPk(matchPk);
        List<Player> playerList=playerService.select(player);
        mv.addObject("playerList", playerList);
        List<Match> matchList=matchService.select(match);
        mv.addObject("matchList", matchList);
        List<MatchPkInfo> matchPkList=matchPkInfoService.selectMatchPkInfo(matchPkInfo);
        mv.setViewName("selectMatchPk");
        mv.addObject("matchPkList", matchPkList);
        return mv;
    }

    @RequestMapping("/addMatchPk")
    public ModelAndView addMatchPk(MatchPkInfo matchPkInfo,ModelAndView mv,Player player, Match match){
        List<Player> playerList=playerService.select(player);
        mv.addObject("playerList", playerList);
        List<Match> matchList=matchService.select(match);
        mv.addObject("matchList", matchList);
        matchPkInfoService.insert(matchPkInfo);
        mv.setViewName("addMatchPk");
        return mv;
    }

    @RequestMapping("/updateMatchPk")
    public ModelAndView matchPkDao(ModelAndView mv,Player player,Match match,String battleId){
        List<Player> playerList=playerService.select(player);
        mv.addObject("playerList", playerList);
        List<Match> matchList=matchService.select(match);
        mv.addObject("matchList", matchList);
        MatchPkInfo matchPkInfo=matchPkInfoService.selectById(battleId);
        mv.addObject("matchPkInfo", matchPkInfo);
        mv.setViewName("updateMatchPk");
        return mv;
    }

    @RequestMapping("/saveMatchPk")
    public ModelAndView addMatchPk(ModelAndView mv,MatchPkInfo matchPkInfo){
        matchPkInfoService.insert(matchPkInfo);

        return selectMatchPk(null, mv, null, null);
    }

    @RequestMapping("/updateBattleFlag")
    public String updateBattleFlag(MatchPkInfo matchPkInfo){
        matchPkInfoService.updateFlag(matchPkInfo);
        return "redirect:/Mpk/selectMatchPk";
    }



}
